<?php
namespace app\admin\controller;

class TplMsg extends Base{
    //
    protected function initialize() {
        parent::initialize();
        $this->model = 'TplMsg';
    }
    //
    protected function _before_index(){
        $thead = [
            'tmid' => 'ID',
            'title' => '短信标题',
            'msg' => '默认短信',
            'xfrom' => '优先时段',
            'xmsg' => '优先短信',
            'platform' => '短信平台',
            'status' => '使用状态',
            'addtime' =>'创建时间',
        ];
        $this->assign('thead', $thead);
    }
    //
    protected function _filter($map){
        return $map;
    }
    //
    protected function _after_list($list){
        foreach($list as $key => $row){
            $list[$key]['xfrom'] = $row['xfrom'].'-'.$row['xto'];
            $list[$key]['status'] = $row['status'] == '1' ? '启用' : '禁用';
            $list[$key]['platform'] = $row['platform'] == 'yunpian' ? '云片短信' : '其他';
            $list[$key]['addtime'] = date('Y-m-d H:i:s', $row['addtime']);
        }
        return $list;
    }
    //
    protected function _before_add(){
        $hour = ['00','01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23'];
        $minute = [];
        for($i = 0; $i < 60; $i++){
            $minute[] = $i < 10 ? '0'.$i : $i;
        }
        $this->assign('hour', $hour);
        $this->assign('minute', $minute);
    }
    //
    protected function _before_insert($data){
        $data = $this->_before_save($data);
        $data['addtime'] = time();
        return $data;
    }
    //
    protected function _after_add($data, $id){
        $this->log(['pk' => 'tmid', 'value' => $id], $data);
    }
    //
    protected function _before_edit(){
        $this->_before_add();
    }
    //
    protected function _filter_edit($list){
        $from = explode(':', $list['xfrom']);
        $list['fromh'] = $from[0] ?? '00';
        $list['fromm'] = $from[1] ?? '00';
        $to = explode(':', $list['xto']);
        $list['toh'] = $to[0] ?? '00';
        $list['tom'] = $to[1] ?? '00';
        return $list;
    }
    //
    protected function _before_save($data){
        $data['xfrom'] = $data['fromh'].':'.$data['fromm'];
        $data['xto'] = $data['toh'].':'.$data['tom'];
        unset($data['fromh'], $data['fromm'], $data['toh'], $data['tom']);
        return $data;
    }
    //
    protected function _after_edit($data){
        $this->log(['pk' => 'tmid', 'value' => $data['tmid']], $data);
    }
    //
    protected function _filter_del(){
        return [1,2];
    }
    //
    protected function _after_del($rows){
        $logs = [];
        foreach($rows as $row){
            $logs[] = ['pk' => 'tmid', 'value' => $row['tmid'], 'row' => $row];
        }
        $this->logAll($logs);
    }
}